package com.microservice.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Produces;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/25 0025.
 */
@ControllerAdvice
public class globalExceptionHandler {

    public static final String DEFAULT_ERROR_VIEW = "error";

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    @Produces("application/json")
    public Map<String,Object> defaulErrorHandel(HttpServletRequest request, Exception e) throws  Exception{
        Map<String,Object> resmap=new HashMap<String,Object>();
        System.out.println("捕获到异常："+e.getMessage());
        if(e.getMessage().indexOf("Incorrect result size")>-1){
            resmap.put("errorInfo","关联字段在关联表中未查询到记录");
        }else if(e.getMessage().indexOf("NumberFormatException")>-1){
            String temp = e.getMessage().toString().split(":")[2];
            resmap.put("errorInfo","所传参数格式不对!");
            resmap.put("errorData",temp);
        }
        resmap.put("exception",e.toString());
        resmap.put("success",false);
        e.printStackTrace();
        return resmap;
    }
}
